package com.jtlrm.ckd.mvp.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.base.sdk.base.net.CommonObserver;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.config.UrlConstants;
import com.jtlrm.ckd.entity.PageEntity;
import com.jtlrm.ckd.entity.YingYangEntity;
import com.jtlrm.ckd.entity.event.ActivityFinishMessage;
import com.jtlrm.ckd.mvp.model.YingYangBaoGaoModel;
import com.jtlrm.ckd.mvp.view.activity.YingBaoNewDetailActivity;
import com.jtlrm.ckd.mvp.view.adapter.YingYangBaogaoAdapter;
import com.jtlrm.ckd.util.EventBusUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

/**
 * 营养报告
 */
public class YinYangBaoGaoFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "patientId";

    // TODO: Rename and change types of parameters
    private String patientId;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    YingYangBaoGaoModel baoGaoModel;
    private int pageNum = 1;
    private int pageSize = 12;
    YingYangBaogaoAdapter adapter;

    public YinYangBaoGaoFragment() {

    }


    public static YinYangBaoGaoFragment newInstance(String patientId) {
        YinYangBaoGaoFragment fragment = new YinYangBaoGaoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, patientId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            patientId = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_yin_yang_bao_gao;
    }

    @Override
    protected void initView() {
        baoGaoModel = new YingYangBaoGaoModel();
        adapter = new YingYangBaogaoAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        View header = LayoutInflater.from(context).inflate(R.layout.yingyang_baogao_data_header, null);
        adapter.addHeaderView(header);
        header.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YingBaoNewDetailActivity.goDetail(context, UrlConstants.ADD_YING_YANG_WEB + "pid=" + patientId, "");
            }
        });
        recyclerView.addItemDecoration(createItemDecoration());
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        adapter.openLoadAnimation();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNum++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                loadData();
            }
        });
        smartRefreshLayout.autoRefresh();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                YingYangEntity entity = (YingYangEntity) adapter.getData().get(position);
                YingBaoNewDetailActivity.goDetail(context, UrlConstants.YING_YANG + "id=" + entity.getBusinessReportId(), "营养报告查看");
            }
        });
    }

    private void loadData() {
        baoGaoModel.queryRecorderList(pageNum, pageSize, patientId, new CommonObserver<PageEntity<List<YingYangEntity>>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                closeFrshorLoadmore();
            }

            @Override
            public void onResult(PageEntity<List<YingYangEntity>> data) {
                closeFrshorLoadmore();
                if (pageNum > 1) {
                    adapter.addData(data.getItems());
                } else {
                    adapter.replaceData(data.getItems());
                }
                if (data.getItems().size() < pageSize) {
                    smartRefreshLayout.setEnableLoadmore(false);
                } else {
                    smartRefreshLayout.setEnableLoadmore(true);
                }
            }
        }, lifecycleSubject);
    }

    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    protected RecyclerView.ItemDecoration createItemDecoration() {
        return new DefaultItemDecoration(ContextCompat.getColor(context, R.color.line_color), 2, 1, -1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ActivityFinishMessage message) {
        if (message != null) {
            loadData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
